package com.attendancesystem.controller;

import com.attendancesystem.config.LocalDateAdapter;
import com.attendancesystem.config.StatusAdapter;
import com.attendancesystem.model.Attendance;
import com.attendancesystem.util.JDBCUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;



import javax.servlet.annotation.WebServlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name="AttendanceRecordController",value = "/AttendanceRecordController")
public class AttendanceRecordController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
//        request.setCharacterEncoding("UTF-8;charset=UTF-8");
        HttpSession session = request.getSession();
        String studentId = (String) session.getAttribute("studentId");
        List<Attendance> attendanceList = new ArrayList<>();
//        System.out.println("studentId:"+studentId);
        Attendance attendance = null;
        String sql="SELECT student_id, attendance_date,course_name,status FROM attendance Where student_id like '%" + studentId + "%' ORDER BY attendance_date DESC LIMIT 5";

        try(Connection con = JDBCUtils.getConnection(); // 必须从连接池获取
            PreparedStatement stm = con.prepareStatement(sql)) {
            ResultSet res = stm.executeQuery();
            while (res.next()){
                attendance=new Attendance(
                        res.getString("student_id"),
                        res.getString("course_name"),
                        res.getDate("attendance_date").toLocalDate(),
                        Attendance.Status.fromChineseName(res.getString("status"))
                );
                attendanceList.add(attendance);
//                System.out.println(Attendance.Status.fromChineseName(res.getString("status")));
            }
            Gson gson = new GsonBuilder()
                    .registerTypeAdapter(LocalDate.class, new LocalDateAdapter())
                    .registerTypeAdapter(Attendance.Status.class, new StatusAdapter())// 添加适配器
                    .serializeNulls()
                    .create();
            String json = attendanceList.isEmpty() ? "[]" : gson.toJson(attendanceList);

            // 调试输出
//            System.out.println("返回JSON：" + json);

            response.getWriter().write(json);
            response.getWriter().flush();
//            Gson gson = new Gson();
//            response.getWriter().write(gson.toJson(attendanceList));


        }catch (Exception e){
            System.out.println(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           doGet(request,response);
    }
}
